package com.kgc.business.businessmanagerweb.controller;

import com.kgc.business.bean.Users;
import com.kgc.business.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UsersController {

    @Reference
    UserService userService;
    @RequestMapping("/dologin")

    @ResponseBody
    public Map<String, Object> dologin(@RequestParam(value = "name",required = false,defaultValue = "") String name,
                          @RequestParam(value = "password",required = false,defaultValue = "") String password){
        Map<String, Object> dologinMap = userService.dologin(name,password);
        return dologinMap;
    }

    Users users;
    @RequestMapping("/tomain")
    public String tomain(Integer id, Model model){
         users = userService.seleById(id);
        model.addAttribute("user",users);
        return "main";
    }

    @RequestMapping("/tuichu")
    public String tuichu(){
     return "../static/login.html";
    }

    @RequestMapping("/seleUsers")
    @ResponseBody
    public String seleUsers(@RequestParam(value = "name",required = false,defaultValue = "")String name,
                            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum){
        String s = userService.seleUsers(name, pageNum);
        return s;
    }


    @RequestMapping("/seleusersByPassword")
   @ResponseBody
    public String seleusersByPassword(String password){
        String users = userService.getUsers(password);
        return users;
    }

    @RequestMapping("/doup")
    @ResponseBody
    public String doup(String password){
        users.setPassword(password);
        Integer integer = userService.updateUsers(users);
        if(integer>0){
            return "SUCCESS";
        }else{
            return "FAIL";
        }
    }

}
